package com.example.smart.widget

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText

class VerticalEditText(context: Context, attrs: AttributeSet) : AppCompatEditText(context, attrs) {

    // 重写 onDraw 方法，垂直绘制文本
    override fun onDraw(canvas: Canvas) {
        val text = text.toString()
        if (text.isEmpty()) return

        // 保存当前 Canvas 状态
        canvas.save()

        // 旋转 Canvas 90 度，实现垂直方向
        canvas.rotate(90f, width / 2f, height / 2f)

        // 计算每个字符的位置
        val paint = paint
        paint.color = currentTextColor
        paint.textSize = textSize

        val charHeight = paint.fontMetrics.let { it.descent - it.ascent }
        var yPos = height / 2f - (text.length * charHeight) / 2f

        // 逐个绘制字符（从上到下）
        for (char in text) {
            val xPos = (width - paint.measureText(char.toString())) / 2f
            canvas.drawText(char.toString(), xPos, yPos, paint)
            yPos += charHeight
        }

        // 恢复 Canvas 状态
        canvas.restore()
    }
}